package com.wsc.wisdomchina.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.javaBean.ConvenienceModuleListBean;
import com.wsc.wisdomchina.javaBean.HomeModule;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 四大功能适配器
 * Created by ${wangtao} on 2017/2/24.
 */

public class ConvenienceAdapter extends BaseAdapter {
    private List<ConvenienceModuleListBean.ConvenienceModule.ConvenienceItem> objects;
    private Context context;
    private LayoutInflater layoutInflater;
    private boolean isNew;

    private static Map<String,Integer> mResources = new HashMap<String,Integer>();

static {
    mResources.put("bsck",R.drawable.bsck);
    mResources.put("bxjd",R.drawable.bxjd);
    mResources.put("fwzl",R.drawable.fwzl);
    mResources.put("jypx",R.drawable.jypx);
    mResources.put("lyxx",R.drawable.lyxx);
    mResources.put("qyxx",R.drawable.qyxx);
    mResources.put("qzzp",R.drawable.qzzp);
    mResources.put("shjf",R.drawable.shjf);
    mResources.put("sjcx",R.drawable.sjcx);
    mResources.put("zcfw",R.drawable.zcfw);
    mResources.put("zlfw",R.drawable.zlfw);
}
    public ConvenienceAdapter(Context context, List<ConvenienceModuleListBean.ConvenienceModule.ConvenienceItem> objects, boolean isNew) {
        this.objects = objects;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.isNew=isNew;
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public ConvenienceModuleListBean.ConvenienceModule.ConvenienceItem getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_convenience_view, null);
            int height = Tools.dip2px(context,86);
            convertView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,height));
            convertView.setTag(new ConvenienceAdapter.ViewHolder(convertView));
        }
        initializeViews((ConvenienceModuleListBean.ConvenienceModule.ConvenienceItem) getItem(position), (ConvenienceAdapter.ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(ConvenienceModuleListBean.ConvenienceModule.ConvenienceItem object, ConvenienceAdapter.ViewHolder holder) {
        //TODO implement
/*        Glide.with(context).load(object.getIcon())
                .placeholder(R.mipmap.own_pic_logout)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(holder.itemHeadImg);*/
        if(isNew)
        {
            holder.itemName.setText(object.getName());
            holder.itemContent.setText(object.getSubtitle());
        }else
        {
            holder.itemName.setText(object.getName());
            holder.itemContent.setText(object.getSubtitle());
        }
     // holder.itemImg.setImageResource(mResources.get(object.getShortName()));
        if(mResources.containsKey(object.getShortName())){
            holder.itemImg.setBackgroundResource(mResources.get(object.getShortName()));
        }else{
            holder.itemImg.setBackgroundResource(R.drawable.default_image);
        }
    }

    protected class ViewHolder {
        private ImageView itemImg;
        private TextView itemName;
        private TextView itemContent;

        public ViewHolder(View view) {
            itemImg = (ImageView) view.findViewById(R.id.item_back_img);
            itemName = (TextView) view.findViewById(R.id.item_title);
            itemContent = (TextView) view.findViewById(R.id.item_content);
        }
    }
}
